package String.homework;

import java.util.Random;
import java.util.Scanner;

/**
 * @author Justin Parker
 * @author 2024/7/11 23:46
 * @version 1.0
 */
public class Test06 {
    public static void main(String[] args){
        //确定验证码生成的内容范围
        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        //验证码，默认为空，并且“”相连
        String code = "";
        //随机抽取line中的字符下标
        Random random = new Random();
        //循环抽取4次line的字符下标
        for(int i=0;i<4;i++){
            //生成随机数
            int index = random.nextInt(line.length());
            //获取下标对应的字符
            char c = line.charAt(index);
            //将字符拼接成字符串
            code += c;
        }
        //输出验证码
        System.out.println("验证码:"+code);
        //输入验证码
        System.out.println("请输入验证码:");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        //比较输入和生成的验证码是否相等
        if(code.equalsIgnoreCase(input)){
            System.out.println("验证码正确！");
        }else{
            System.out.println("验证码错误！");
        }
    }
}
